package com.cxb.view;

import android.content.Context;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cxb.R;
import com.cxb.manager.CxbEventBus;
import com.cxb.manager.PhoneManager;
import com.cxb.manager.PhoneManager.PhoneState;

public class PhoneDialogView extends View implements Callback, OnTouchListener,
		OnClickListener {

	private Context mContext = null;
	private WindowManager mWM = null;
	public Handler mHandler = null;
	private WindowManager.LayoutParams mWMParams = null;
	private PhoneManager phoneManager = null;
	private TextView tv_calling_tip = null;
	private View ll_bungUp = null;
	private View ll_diapad = null;
	private View ll_anwser = null;
	private DiapadDialog diapadDialog = null;

	public static final int UPDATEVIEW = 20131019;
	public static PhoneDialogView phoneActiveDialogView = null;
	public static View activeView = null;

	public static PhoneDialogView getInstance(Context context) {
		if (phoneActiveDialogView == null) {
			phoneActiveDialogView = new PhoneDialogView(context);
		}
		return phoneActiveDialogView;
	}

	private PhoneDialogView(Context context) {

		super(context);
		mContext = context;

		phoneManager = new PhoneManager(context);
		diapadDialog = new DiapadDialog(context);
		mWM = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

		mHandler = new Handler(new Callback() {
			@Override
			public boolean handleMessage(Message msg) {
				if (msg.what == UPDATEVIEW) {
					Log.i("test", "D"+PhoneManager.getCallState());
					if (PhoneManager.getCallState() == PhoneState.DIALING) {
						String number = PhoneManager.getConectingPhoneNumber();
						if (number != null && tv_calling_tip != null) {
							tv_calling_tip.setText(phoneManager.getContactName(number));
						}
					} else if (PhoneManager.getCallState() == PhoneState.ACTIVE) {
						ll_bungUp.setVisibility(View.VISIBLE);
						if (tv_calling_tip != null) {
							long activeTime = PhoneManager.getActiveTime();
							String sTime = String.format("%02d:%02d:%02d",
									activeTime / 3600, activeTime % 3600 / 60,
									activeTime % 60);
							tv_calling_tip.setText(sTime);
							
						}

						if (ll_anwser != null
								&& ll_anwser.getVisibility() == View.VISIBLE) {
							ll_anwser.setVisibility(View.GONE);
						}

						if (ll_diapad != null
								&& ll_diapad.getVisibility() != View.VISIBLE) {
							ll_diapad.setVisibility(View.VISIBLE);
						}
					} else if (PhoneManager.getCallState() == PhoneState.RING) {
						String number = PhoneManager.getConectingPhoneNumber();
						if (number != null && tv_calling_tip != null) {
							tv_calling_tip.setText(phoneManager.getContactName(number));
						}
//						ll_bungUp.setVisibility(View.GONE);
						if (ll_diapad != null
								&& ll_diapad.getVisibility() == View.VISIBLE) {
							ll_diapad.setVisibility(View.GONE);
						}

						if (ll_anwser != null
								&& ll_anwser.getVisibility() != View.VISIBLE) {
							ll_anwser.setVisibility(View.VISIBLE);
						}
					}
				}
				return false;
			}
		});
	}

	public void show() {

		if (activeView != null) {
			return;
		}

		activeView = LayoutInflater.from(mContext).inflate(
				R.layout.phone_view_layout, null);
		activeView.setOnTouchListener(this);
		ll_diapad = (View) activeView.findViewById(R.id.btn_dtmf);
		ll_diapad.setOnClickListener(this);
		ll_diapad.setVisibility(View.GONE);
		 ll_anwser = (View) activeView.findViewById(R.id.btn_answer);
		 ll_anwser.setOnClickListener(this);
		 ll_anwser.setVisibility(View.GONE);
		ll_bungUp = (View) activeView.findViewById(R.id.btn_hangup);
		ll_bungUp.setOnClickListener(this);
		tv_calling_tip = (TextView) activeView
				.findViewById(R.id.tv_calling_tip);
		tv_calling_tip.setTextSize(18);

		DisplayMetrics dm = new DisplayMetrics();
		dm = mContext.getResources().getDisplayMetrics();

		mWMParams = new WindowManager.LayoutParams();
		mWMParams.type = WindowManager.LayoutParams.TYPE_PHONE;

		mWMParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_FULLSCREEN;
		mWMParams.flags &= ~40;
		mWMParams.gravity = Gravity.LEFT | Gravity.BOTTOM;
		mWMParams.x = dm.widthPixels;
		mWMParams.y = 0;
		mWMParams.width = LayoutParams.WRAP_CONTENT;
		mWMParams.height = LayoutParams.WRAP_CONTENT;
		mWM.addView(activeView, mWMParams);

		new Thread(new Runnable() {

			@Override
			public void run() {
				while (PhoneManager.getCallState() != PhoneManager.PhoneState.IDLE) {
					try {
						Message msg = mHandler.obtainMessage();
						msg.what = UPDATEVIEW;
						mHandler.sendMessage(msg);

						Thread.sleep(500);
					} catch (Exception e) {
						e.printStackTrace();
					}
					Log.d("llx", "sleep");
				}
				dimissView();
			}
		}).start();
	}

	float x = 0, y = 0;
	float mTouchX = 0, mTouchY = 0;

	public boolean onTouch(View v, MotionEvent event) {

		// 获取相对屏幕的坐标，即以屏幕左上角为原点
		x = (int) event.getRawX();
		y = (int) event.getRawY();

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: // 捕获手指触摸按下动作
			// 获取相对View的坐标，即以此View左上角为原点
			mTouchX = event.getX();
			mTouchY = event.getY();

			break;
		case MotionEvent.ACTION_MOVE: // 捕获手指触摸移动动作
			move(x, y);
			break;
		case MotionEvent.ACTION_UP: // 捕获手指触摸离开动作
			move(x, y);
			mTouchX = mTouchY = 0;

			break;
		}

		return true;
	}

	public void move(float x, float y) {
		if (activeView == null)
			return;

		DisplayMetrics dm = new DisplayMetrics();
		dm = mContext.getResources().getDisplayMetrics();

		mWMParams.x = (int) (x - mTouchX);
		mWMParams.y = dm.heightPixels - (int) (y - mTouchY)
				- activeView.getHeight();
		mWM.updateViewLayout(activeView, mWMParams);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == ll_bungUp.getId()) {// 挂机
			phoneManager.endCall();
			dimissView();
		} else if (v.getId() == ll_diapad.getId()) {
			diapadDialog.show();
		} else if (v.getId() == ll_anwser.getId()) {
			phoneManager.answerRingingCall();
		}
	}

	public void dimissView() {
		try {
			if (activeView != null) {
				mWM.removeView(activeView);
				activeView = null;
				diapadDialog.dismiss();
			}
		} catch (Exception e) {
		}
	}

//	public void setUpOutGoingListener(){
//		CxbEventBus.resgist(CxbEventBus.EVENT_PHONE_STATE_CHANGE, this);
//	}
	@Override
	public boolean handleMessage(Message msg) {
		// TODO Auto-generated method stub
		return false;
	}
}
